<template>
    <div id="CarSharingUser">
        <ul id="ul1">
          <li>
            <el-input v-model="userName" placeholder="请输入"></el-input>
          </li>
          <li>
            <el-button type="primary" @click="queryUserByName">查询</el-button>
          </li>
        </ul>
        <!-- 表格 -->
        <el-table :row-style="rowStyle" :data="userData" border style="width: 100%;" height="500"  >
          <el-table-column fixed  label="头像">
            <template slot-scope="scope">
                <el-avatar size="medium" :src="'http://101.43.223.224:8765/image/getImageById?id='+scope.row.avatarId"></el-avatar>
            </template>
          </el-table-column>
          <el-table-column prop="userName" label="用户名"></el-table-column>
          <el-table-column prop="loginName" label="登录名"></el-table-column>

          <el-table-column prop="email" label="管理员邮箱" min-width="200" ></el-table-column>
          <el-table-column prop="phone" label="手机号" min-width="200" ></el-table-column>
          <el-table-column prop="age" label="年龄" min-width="200" ></el-table-column>
          <el-table-column prop="lastLoginTime" label="最后登录时间" min-width="200" ></el-table-column>
          <el-table-column prop="lastLoginIp" label="最后登录ip" min-width="200" ></el-table-column>
    
          <!-- <el-table-column prop="workTime" label=""></el-table-column> -->
          <el-table-column prop="createTime" label="创建时间" min-width="120" ></el-table-column>
          <el-table-column prop="updateTime" label="修改时间" min-width="120" ></el-table-column>
   
  
          <el-table-column fixed="right" label="操作" width="290">
            <template slot-scope="scope">

              <el-button @click="huifu(scope.row)" type="success" size="small" >恢复</el-button>
              <el-button @click="deleteUserById(scope.row)" type="danger" size="small">彻底删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[1, 2, 3, 10]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          :background="true"
          style="margin-top: 30px">
        </el-pagination>
      <!-- 添加弹出框 -->
      <el-dialog title="添加员工信息" :visible.sync="userAddWindow" width="30%">
        <el-form
          :model="userForm"
          ref="userForm"
          label-width="100px"
          class="demo-ruleForm">
          <el-form-item label="头像" >
            <el-upload
  class="avatar-uploader"
  action="https://jsonplaceholder.typicode.com/posts/"
  :show-file-list="false"
  :before-upload="beforeAvatarUpload">
  <img height="200" width="200" v-if="imageUrl" :src="imageUrl" class="avatar">
  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
</el-form-item>
          <el-form-item label="用户名" prop="userName">
            <el-input v-model="userForm.userName"></el-input>
          </el-form-item>
          <el-form-item label="用户名" prop="loginName">
            <el-input v-model="userForm.loginName"></el-input>
          </el-form-item>
          <el-form-item label="管理员邮箱" prop="email">
            <el-input v-model="userForm.email"></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="userForm.phone"></el-input>
          </el-form-item>
          <el-form-item label="年龄" prop="age">
            <el-input type="number" v-model="userForm.age"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="userAddWindow = false">取 消</el-button>
          <el-button @click="resetForm('userForm')">重置</el-button>
          <el-button type="primary" @click="addUserInfo('userForm')">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 详情弹出框 -->
      <el-dialog title="修改病虫害信息" :visible.sync="userInfoWindow" width="30%">
        <el-form :model="userInfo" ref="userForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="头像" >
            <el-upload
  class="avatar-uploader"
  action="https://jsonplaceholder.typicode.com/posts/"
  :show-file-list="false"
  :before-upload="beforeAvatarUpload">
  <img height="200" width="200" v-if="imageUrl" :src="imageUrl" class="avatar">
  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
</el-form-item>
          <el-form-item label="用户名" prop="userName">
            <el-input v-model="userInfo.userName"></el-input>
          </el-form-item>
          <el-form-item label="用户名" prop="loginName">
            <el-input v-model="userInfo.loginName"></el-input>
          </el-form-item>
          <el-form-item label="管理员邮箱" prop="email">
            <el-input v-model="userInfo.email"></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="userInfo.phone"></el-input>
          </el-form-item>
          <el-form-item label="年龄" prop="age">
            <el-input type="number" v-model="userInfo.age"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="userInfoWindow = false">取 消</el-button>
          <el-button @click="resetForm('userForm')" :disabled="disable">重置</el-button>
          <el-button @click="updateUserInfo('userForm')" :disabled="disable" type="primary">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </template>
  
  <script>
  export default {
    name: 'CarSharingUser',
    data () {
      return {
        disable: true,
        userName: '', // 用户姓名
        userData: [], // 员工查询数据
        userAddWindow: false, // 员工添加窗口控制
        userInfoWindow: false, // 员工信息窗口控制
        userForm: {
          // 员工添加对象
          userName: '', // 员工名称
          age: '', // 年龄
          address: 1, // 地址
          email: '', // 邮箱
        },
        imageUrl:'',
        file:'',
        userInfo: {}, // 员工信息
        pageSize: 10, // 每页显示的个数
        pageNum: 1, // 起始的页数 表格中当前页面
        total: 0
      }
    },
    mounted () {
      this.queryUserByName()
    },
    methods: {
        huifu(row){
this.userInfo={}
this.userInfo.id=row.id
this.userInfo.userFlag=0
this.updateUserInfo()
        },
        rowStyle (row) {
            console.log(row)
      if(row.row.userFlag==1){
        return {'background-color':'rgb(233 233 233)'}
      }
    },
        aaaain(a){
            console.log(a)
            this.userInfo={}
            this.userInfo.id=a.id
            this.userInfo.userFlag=a.userFlag
            this.updateUserInfo()
        },
        beforeAvatarUpload(file){
            this.file=file
            this.imageUrl=URL.createObjectURL(file)

return false
        },
      handleSizeChange (val) {
        this.pageSize = val
        this.queryUserByName()
        console.log(`每页 ${val} 条`)
      },
      handleCurrentChange (val) {
        this.pageNum = val
        this.queryUserByName()
        console.log(`当前页: ${val}`)
      },
      updateUserInfo (formName) {
          console.log(this.userInfo)
          var formdata = new FormData()
            for(var a in this.userInfo){
                if(this.userInfo[a]!=null){
                    formdata.set(a,this.userInfo[a])
                }
            }
            if(this.file){
                formdata.set('avatar',this.file,this.file.filename)
            }
            this.$axios
              .put(
                'sysUser/updateUser',
                formdata
              )
              .then((res) => {
                if (res.status === 200) {
                  this.myMessage('success', '恭喜您！成功修改了一条信息！')
                  this.userInfoWindow = false
                  this.queryUserByName()
                } else {
                  this.myMessage('warning', '警告喔！您有一条信息修改失败！')
                }
              })
            
      },
      openUserInfoWindow (row, windowType) {
        // 打开修改或详情窗口
        this.userInfo = JSON.parse(JSON.stringify(row))
        this.disable = windowType === 'getInfo'
        this.imageUrl=
        this.file = 'http://101.43.223.224:8765/image/getImageById?id='+this.userInfo.avatarId
        this.userInfoWindow = true
      },
      deleteUserById (id) {
        // 删除信息
        this.$confirm('此操作将彻底删除该员工信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            this.$axios
            .delete('sysUser/delUserById', {
              params: {
                id: id.id
              }
            })
            .then((res) => {
              if (res.status === 200) {
                this.myMessage('success', '您成功删除了一条信息！')
                this.queryUserByName()
              } else {
                this.myMessage('warning', '警告喔！您有一条信息删除失败！')
              }
            })
          
          })
          .catch((error) => {
            console.log(error)
            this.myMessage('info', '已取消')
          })
      },
      queryUserByName () {
        // 查询员工信息
        this.$axios
          .get('sysUser/getUserRetrieveByPage', {
            params: {
              userName: this.userName,
              pageNum: this.pageNum,
              pageSize: this.pageSize
            }
          })
          .then((res) => {
            if (res.status === 200) {
              this.userData = res.data.data.content
              this.total = res.data.data.totalSize
            }
          })
          .catch((error) => {
            this.myMessage('error', '错误哦！系统错误，请联系管理员！')
            console.log(error)
          })
      },
      addUserInfo (formName) {
        // 添加
        this.$refs[formName].validate((valid) => {
          if (valid) {
            var formdata = new FormData()
            for(var a in this.userForm){
                if(this.userForm[a]){
                    formdata.set(a,this.userForm[a])
                }
            }
            if(this.file){
                formdata.set('avatar',this.file,this.file.filename)
            }
            this.$axios
              .post(
                'sysUser/saveUser',
                formdata
              )
              .then((res) => {
                if (res.status === 200) {
                  this.myMessage('success', '恭喜您！成功添加了一条信息！')
                  this.userAddWindow = false
                  this.$refs[formName].resetFields()
                  this.queryUserByName()
                } else {
                  this.myMessage('warning', '警告喔！您有一条信息添加失败！')
                }
              })
              .catch((error) => {
                this.myMessage('error', '错误哦！系统错误，请联系管理员！')
                console.log(error)
              })
          } else {
            return false
          }
        })
      },
      // 重置表单
      resetForm (formName) {
        this.$refs[formName].resetFields()
      },
      openAddWindow () {
        this.file = ''

        this.userForm.createUser=JSON.parse(window.localStorage.getItem('userInfo')).id
        this.userForm.updateUser=JSON.parse(window.localStorage.getItem('userInfo')).id
        // 打开添加窗口
        this.userAddWindow = true
      },
      // 提示信息
      myMessage (type, message) {
        this.$message({
          showClose: true,
          message: message,
          type: type
        })
      },
      // 表格文字居中
     
      // 过滤树
      filterNode (value, data) {
        if (!value) return true
        return data.menuName.indexOf(value) !== -1
      }
    }
  }
  </script>
  
  <style lang="scss" scoped>
#ul1{
    height: 40px;
    margin-bottom: 15px;
    list-style: none;

  }
  #ul1 li{
    float: left;
    margin-right: 10px;
  }
  </style>
  